﻿'*******************************************************************************************
'* Copyright Brügmann Software GmbH, Papenburg
'* Author: Björn Kremer
'* Contact: codeplex<at>patorg.de
'* 
'* VB.CMIS is a VB.NET implementation of the Content Management Interoperability Services (CMIS) standard
'*
'* This file is part of VB.CMIS.
'* 
'* VB.CMIS is free software: you can redistribute it and/or modify
'* it under the terms of the GNU Lesser General Public License as published by
'* the Free Software Foundation, either version 3 of the License, or
'* (at your option) any later version.
'* 
'* VB.CMIS is distributed in the hope that it will be useful,
'* but WITHOUT ANY WARRANTY; without even the implied warranty of
'* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'* GNU Lesser General Public License for more details.
'* 
'* You should have received a copy of the GNU Lesser General Public License
'* along with VB.CMIS. If not, see <http://www.gnu.org/licenses/>.
'*******************************************************************************************
Imports System
Imports System.Runtime.Serialization
Imports System.Xml.Linq
Imports System.Xml.Serialization
Imports System.Xml

Namespace PropertyDefinitions.Choices
   <XmlRoot("choice", Namespace:=CmisNamespaceConstants.Cmis)>
   Public Class CmisChoice(Of ValueType)
      Implements IXmlSerializable

      Public Property DisplayName As String

      Public Property Values As ValueType()

      Public Property Choices As New List(Of CmisChoice(Of ValueType))


      Public Function GetSchema() As System.Xml.Schema.XmlSchema Implements System.Xml.Serialization.IXmlSerializable.GetSchema
         Return Nothing
      End Function

      Public Sub ReadXml(reader As System.Xml.XmlReader) Implements System.Xml.Serialization.IXmlSerializable.ReadXml
         Dim doc As New System.Xml.XmlDocument()
         doc.Load(reader)

         Dim node As System.Xml.XmlNode = Common.GetItemCaseInsensitiveFromXML(doc, Common.GetXMLRootName(Me.GetType).ToLowerInvariant)
         For Each attr As System.Xml.XmlAttribute In node.Attributes
            Dim lName As String = Common.GetNodeNameWithoutPrefix(attr).ToLowerInvariant
            If lName.Equals("displayname") Then
               Me.DisplayName = attr.InnerText
               Exit For
            End If
         Next
         Dim values As New List(Of ValueType)
         Dim choices As New List(Of CmisChoice(Of ValueType))
         For Each child As System.Xml.XmlElement In node.ChildNodes
            Dim lName As String = Common.GetNodeNameWithoutPrefix(child).ToLowerInvariant
            If lName.Equals("value") Then
               values.Add(DirectCast(Convert.ChangeType(child.InnerText, GetType(ValueType)), ValueType))
            ElseIf lName.Equals("choice") Then
               Using stream As New IO.StringReader(child.OuterXml)
                  Dim serializer As New Serialization.XmlSerializer(GetType(CmisChoice(Of ValueType)))
                  choices.Add(DirectCast(serializer.Deserialize(stream), CmisChoice(Of ValueType)))
               End Using
            End If
         Next
         Me.Values = values.ToArray
         Me.Choices = choices
      End Sub

      Public Sub WriteXml(writer As System.Xml.XmlWriter) Implements System.Xml.Serialization.IXmlSerializable.WriteXml
         Dim ns As String = CmisNamespaceConstants.Cmis
         writer.WriteAttributeString("displayName", DisplayName)
         If Values IsNot Nothing Then
            For Each value As ValueType In Values
               writer.WriteElementString("value", ns, value.ToString)
            Next
         End If
         For Each c As CmisChoice(Of ValueType) In Choices
            Dim serializer As New XmlSerializer(c.GetType)
            serializer.Serialize(writer, c)
         Next
      End Sub
   End Class
End Namespace